﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace hkshort
{
    class MySpider
    {
        public string WebSite = "http://www.hkex.com.hk/eng/market/sec_tradinfo/stkcdorder.htm";
        public string StartDate = "2011-05-30";
        public string EndDate = "2011-05-30";
        public int PageNo = 1;

        public List<Notice> CollectNotice()
        {
            string content = this.SendGetRequest();
            List<Notice> list = this.ParseWebPage(content);
            return list;
        }

        public string SendGetRequest()
        {
            Encoding myEncoding = Encoding.GetEncoding("utf-8");
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(this.WebSite);
            myRequest.Timeout = 30000;
            myRequest.Headers.Set("Pragma", "no-cache"); 

            string content = "";
            using (HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse())
            {
                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), myEncoding);
                content = reader.ReadToEnd();
                //Console.WriteLine(content);
            }

            return content;
        }


        public List<Notice> ParseWebPage(string content)
        {
            List<Notice> result = new List<Notice>();

            string startTag = "Listed by Stock Code Order";
            string endTag = "Stock exempt from tick rule";
            string tableStartTag = "<table style=\"width";
            string tableEndTag = "</table>";
            string dateTag = "Effective";
            string lineTag = "<p><span";
            int start = content.IndexOf(startTag);
            int end = content.IndexOf(endTag);
            if (start < 0 || end < 0 || start >= end)
            {
                //报错
                //throw new Exception("页面格式找不到开头或结尾标记");
                return result;
            }

            content = content.Substring(start, end - start);
            //Console.WriteLine(content);
            int offset = 0;
            int tableStart = content.IndexOf(tableStartTag, offset);
            int tableEnd = content.IndexOf(tableEndTag, offset);
            int lastTableEnd = 0;
            while (tableStart >= 0 && tableEnd>=tableStart)
            {
                //确定Effective Date
                string strDate = "";
                int dateStart = content.LastIndexOf(dateTag, tableStart, (tableStart - lastTableEnd));
                if (dateStart >= 0)
                {
                    int dateEnd1 = content.IndexOf("<", dateStart);
                    int dateEnd2 = content.IndexOf(",", dateStart);
                    int dateEnd = Math.Min((dateEnd1>=0?dateEnd1:int.MaxValue), (dateEnd2>=0?dateEnd2:int.MaxValue));
                    if (dateEnd > dateStart && dateEnd < int.MaxValue)
                    {
                        strDate = content.Substring(dateStart + dateTag.Length, (dateEnd - dateStart - dateTag.Length)).Trim();
                    }
                }
                //Console.WriteLine(strDate);
                if (strDate != "")
                {
                    strDate = DateTime.Parse(strDate).ToString("yyyyMMdd");
                }
                //Console.WriteLine(strDate);

                //遍历表格找Ticker
                string[] lines = content.Substring(tableStart,tableEnd-tableStart).Split(
                    new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
                int i = 0;
                int no = 0;
                Notice notice = null;
                while (i < lines.Length) 
                {
                    string tmp = "";
                    while ( i<lines.Length && !lines[i].StartsWith(lineTag))   
                    {
                        i++;
                    }
                    if (i < lines.Length)
                    {
                        int fieldStart = lines[i].IndexOf(">", lineTag.Length) + 1;
                        tmp = lines[i].Substring(fieldStart, lines[i].IndexOf("</span>") - fieldStart);
                        no ++;
                    }
                    if (no % 3 == 1)
                    {
                        notice = new Notice();
                        notice.Date = strDate;
                        notice.No = int.Parse(tmp);
                    }
                    else if (no % 3 == 2)
                    {
                        notice.Ticker = tmp;
                    }
                    else if (no % 3 == 0)
                    {
                        notice.Name = tmp;
                        result.Add(notice);
                    }
                    i++;
                }

                //继续寻找table
                offset = tableEnd + tableEndTag.Length;
                lastTableEnd = tableEnd;
                tableStart = content.IndexOf(tableStartTag, offset);
                tableEnd = content.IndexOf(tableEndTag, offset);
            }
            
            return result;
        }
    }
}
